package com.express.e.root.controller;

import com.express.e.root.annotation.ResponseBody;
import com.express.e.root.bean.Delivery;
import com.express.e.root.bean.Message;
import com.express.e.root.enums.DeliveryEnum;
import com.express.e.root.service.DeliveryService;
import com.express.e.root.service.impl.DeliveryServiceImpl;
import com.express.e.root.utils.DeliveryWrap;
import com.express.e.root.utils.FomatDateUtil;
import com.express.e.root.utils.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.sql.Timestamp;

public class DeliveryController {
    private DeliveryService deliveryService = new DeliveryServiceImpl();

    @ResponseBody("/delivery/list.do")
    public String deliveryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        String pageIndex = request.getParameter("offset");
        String pageSize = request.getParameter("pageNumber");
        List<Delivery> deliveries = deliveryService.queryAll();
        List deliverNew = new ArrayList<>();
        for (Delivery d :
                deliveries) {
            DeliveryWrap delivery = new DeliveryWrap();
            delivery.setDeliveryId(d.getDeliveryId());
            delivery.setDeliveryCode(d.getDeliveryCode());
            delivery.setDeliveryCompany(d.getDeliveryCompany());
            delivery.setDeliveryAcceptor(d.getDeliveryAcceptor());
            delivery.setAcceptorPhone(d.getAcceptorPhone());
            delivery.setDeliveryNum(d.getDeliveryNum());
            if (DeliveryEnum.FETCH.state == d.getDeliveryState())
                delivery.setDeliveryState(DeliveryEnum.FETCH.desc);
            else if (DeliveryEnum.STORE.state == d.getDeliveryState())
                delivery.setDeliveryState(DeliveryEnum.STORE.desc);
            delivery.setStateChangeTime(FomatDateUtil.formatDate(d.getStateChangeTime()));
            deliverNew.add(delivery);
        }
        if (deliverNew != null) {
            String toJson = JSONUtil.toJson(new Message(200, "查找成功！", deliverNew));
            System.out.println(toJson);
            return toJson;
        } else {
            String toJson = JSONUtil.toJson(new Message(400, "查找失败！", deliverNew));
            System.out.println(toJson);
            return toJson;
        }
    }

    @ResponseBody("/delivery/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        String deliveryCode = request.getParameter("number");
        String deliveryCompany = request.getParameter("company");
        String deliveryAcceptor = request.getParameter("userName");
        String acceptorPhone = request.getParameter("userPhone");
        Calendar calendar = Calendar.getInstance();
        Delivery delivery = deliveryService.addDelivery(new Delivery(deliveryCompany, deliveryAcceptor, acceptorPhone, deliveryCode, 0,
                new Date(System.currentTimeMillis())));
        if (delivery == null || delivery.getDeliveryId() == 0) {
            return JSONUtil.toJson(new Message(400, "增加失败", null));
        }
        DeliveryWrap deliveryWrap = new DeliveryWrap(
                delivery.getDeliveryId(),
                delivery.getDeliveryCode(),
                delivery.getDeliveryCompany(),
                delivery.getDeliveryAcceptor(),
                delivery.getAcceptorPhone(),
                delivery.getDeliveryNum(),
                delivery.getDeliveryState() == 0 ? DeliveryEnum.STORE.desc : DeliveryEnum.FETCH.desc,
                FomatDateUtil.formatDate(delivery.getStateChangeTime()));
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(deliveryWrap);
        String insert = JSONUtil.toJson(new Message(200, "新增成功", objects));
        System.out.println(insert);
        return insert;
    }

    @ResponseBody("/delivery/findDelivery.do")
    public String findDelivery(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        String number = req.getParameter("number");
        Delivery delivery = deliveryService.queryByNum(number);
        System.out.println(delivery.toString());
        if (null != delivery && (delivery.getDeliveryAcceptor() != null)  && !(delivery.getDeliveryAcceptor().isEmpty())) {
            DeliveryWrap deliveryWrap = new DeliveryWrap(
                    delivery.getDeliveryId(),
                    delivery.getDeliveryCode(),
                    delivery.getDeliveryCompany(),
                    delivery.getDeliveryAcceptor(),
                    delivery.getAcceptorPhone(),
                    delivery.getDeliveryNum(),
                    delivery.getDeliveryState() == 0 ? DeliveryEnum.STORE.desc : DeliveryEnum.FETCH.desc,
                    FomatDateUtil.formatDate(delivery.getStateChangeTime()));
            ArrayList<Object> objects = new ArrayList<>();
            objects.add(deliveryWrap);
            String toJson = JSONUtil.toJson(new Message(200, "成功查寻到此快递！", objects));
            System.out.println(toJson);
            return toJson;
        }
        return JSONUtil.toJson(new Message(400, "未查到此快递！", null));
    }


    @ResponseBody("/delivery/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        String deliveryNum = req.getParameter("deliveryNum");
        String deliveryCompany = req.getParameter("deliveryCompany");
        String acceptorName = req.getParameter("acceptorName");
        String acceptorPhone = req.getParameter("acceptorPhone");
        String deliveryStatus = req.getParameter("deliveryStatus");
        System.out.println(deliveryNum);
        Delivery delivery = deliveryService.queryByNum(deliveryNum);
        System.out.println(delivery);
        if (delivery != null && delivery.getDeliveryAcceptor() != null) {
            Delivery deliveryUpdate = new Delivery(
                    delivery.getDeliveryId(),
                    delivery.getDeliveryCode(),
                    deliveryCompany,
                    acceptorName,
                    acceptorPhone,
                    deliveryNum,
                    deliveryStatus.equals("未签收") ? DeliveryEnum.STORE.state : DeliveryEnum.FETCH.state,
                    new Date(System.currentTimeMillis()));
            Delivery deliveryResult = deliveryService.updateDelivery(deliveryUpdate);
            ArrayList<Object> objects = new ArrayList<>();
            objects.add(deliveryResult);
            String toJson = JSONUtil.toJson(new Message(200, "修改成功", objects));
            System.out.println(toJson);
            return toJson;
        }
        return JSONUtil.toJson(new Message(400, "修改失败", null));
    }

    @ResponseBody("/delivery/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp) {
        String deliveryNum = req.getParameter("deliveryNum");
        Boolean delFlag = deliveryService.deleteDelivery(deliveryNum);
        if (delFlag) {
            String toJson = JSONUtil.toJson(new Message(200, "删除成功", null));
            System.out.println(toJson);
            return toJson;
        }
        String toJson = JSONUtil.toJson(new Message(400, "删除失败", null));
        System.out.println(toJson);
        return toJson;
    }

    @ResponseBody("/delivery/findByDeliveryName.do")
    public String findByUserName(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        String deliveryName = req.getParameter("deliveryName");
        System.out.println(deliveryName);
        if (null == deliveryName||deliveryName.isEmpty()) {
            return JSONUtil.toJson(new Message(401, "收件人为空！", null));
        }
        Delivery delivery = deliveryService.queryByName(deliveryName);
        if (null != delivery && delivery.getDeliveryAcceptor() != null) {
            System.out.println(delivery.toString());
            DeliveryWrap deliveryWrap = new DeliveryWrap(
                    delivery.getDeliveryId(),
                    delivery.getDeliveryCode(),
                    delivery.getDeliveryCompany(),
                    delivery.getDeliveryAcceptor(),
                    delivery.getAcceptorPhone(),
                    delivery.getDeliveryNum(),
                    delivery.getDeliveryState() == 0 ? DeliveryEnum.STORE.desc : DeliveryEnum.FETCH.desc,
                    FomatDateUtil.formatDate(delivery.getStateChangeTime()));
            ArrayList<Object> objects = new ArrayList<>();
            objects.add(deliveryWrap);
            String toJson = JSONUtil.toJson(new Message(200, "成功查寻到此快件接收人！", objects));
            System.out.println(toJson);
            return toJson;
        }
        String toJson = JSONUtil.toJson(new Message(400, "未查到此快件接收人！", null));
        System.out.println(toJson);
        return toJson;
    }


    @ResponseBody("/delivery/findByDeliveryNum.do")
    public String findByDeliveryNum(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        String deliveryNum = req.getParameter("deliveryNum");
        System.out.println(deliveryNum);
        if (deliveryNum.isEmpty()) {
            return JSONUtil.toJson(new Message(401, "快递单号为空！", null));
        }
        Delivery delivery = deliveryService.queryByNum(deliveryNum);

        if (null != delivery && delivery.getDeliveryAcceptor() != null  && !delivery.getDeliveryAcceptor().isEmpty()) {
            System.out.println(delivery.toString());
            DeliveryWrap deliveryWrap = new DeliveryWrap(
                    delivery.getDeliveryId(),
                    delivery.getDeliveryCode(),
                    delivery.getDeliveryCompany(),
                    delivery.getDeliveryAcceptor(),
                    delivery.getAcceptorPhone(),
                    delivery.getDeliveryNum(),
                    delivery.getDeliveryState() == 0 ? DeliveryEnum.STORE.desc : DeliveryEnum.FETCH.desc,
                    FomatDateUtil.formatDate(delivery.getStateChangeTime()));
            ArrayList<Object> objects = new ArrayList<>();
            objects.add(deliveryWrap);
            String toJson = JSONUtil.toJson(new Message(200, "成功查寻到此快递单号！", objects));
            System.out.println(toJson);
            return toJson;
        }
        String toJson = JSONUtil.toJson(new Message(400, "未查到此快递单号！", null));
        System.out.println(toJson);
        return toJson;
    }


    @ResponseBody("/delivery/findByPhone.do")
    public String findByPhone(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        String phone = req.getParameter("phone");
        System.out.println(phone);
        if (phone.isEmpty()) {
            return JSONUtil.toJson(new Message(401, "手机号码为空！", null));
        }
        Delivery delivery = deliveryService.queryByPhone(phone);

        if (null != delivery  && delivery.getDeliveryAcceptor()!=null) {
            System.out.println(delivery.toString());
            DeliveryWrap deliveryWrap = new DeliveryWrap(
                    delivery.getDeliveryId(),
                    delivery.getDeliveryCode(),
                    delivery.getDeliveryCompany(),
                    delivery.getDeliveryAcceptor(),
                    delivery.getAcceptorPhone(),
                    delivery.getDeliveryNum(),
                    delivery.getDeliveryState() == 0 ? DeliveryEnum.STORE.desc : DeliveryEnum.FETCH.desc,
                    FomatDateUtil.formatDate(delivery.getStateChangeTime()));
            ArrayList<Object> objects = new ArrayList<>();
            objects.add(deliveryWrap);
            String toJson = JSONUtil.toJson(new Message(200, "成功查寻到此用户手机！", objects));
            System.out.println(toJson);
            return toJson;
        }
        String toJson = JSONUtil.toJson(new Message(400, "未查到此用户！", null));
        System.out.println(toJson);
        return toJson;
    }

    @ResponseBody("/delivery/test.do")
    public String test(HttpServletRequest request, HttpServletResponse response) {
        return "hello";
    }
}
